package hello.app.dialog;

import hello.app.ContactActivity;
import hello.app.HistoryActivity;
import hello.app.R;
import hello.app.model.ModelManager;
import hello.app.model.MyContactModel;
import hello.app.model.data.CallHistory;
import hello.app.model.data.HelloContact;
import hello.app.model.data.MyContact;
import android.app.AlertDialog.Builder;
import android.content.ContentUris;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.provider.ContactsContract.RawContacts;
import android.telephony.PhoneNumberUtils;
import android.widget.Toast;

public class SelectHistoryBuilder extends Builder implements OnClickListener{
	HelloContact mContact;
	CallHistory mHistory;
	HistoryActivity mActivity;
	int mPostion;
	static final int DETAIL = 0;
	static final int GOOGLE = 1;
	static final int OUTGOING = 2;
	static final int CONTACT = 3;
	static final int DELETE = 4;

	public SelectHistoryBuilder(HistoryActivity activity, HelloContact contact, CallHistory history) {
		super(activity);
		this.mActivity = activity;
		this.mContact = contact;
		this.mHistory = history;
		setItems(R.array.select_history, this);
	}

	public void onClick(DialogInterface dialog, int which) {
		Intent i;
		Uri uri;
		switch(which){
		case DETAIL:
			ContactActivity.startContactActivity(mActivity, mContact);
			break;
		case GOOGLE:
			uri = Uri.parse("http://google.co.jp/search?q=" + PhoneNumberUtils.formatNumber(mContact.getPhone()));
			i = new Intent(Intent.ACTION_VIEW,uri);
			mActivity.startActivity(i);
			break;
		case OUTGOING:
			uri = Uri.parse("tel:" + mContact.getPhone());
			i = new Intent(Intent.ACTION_CALL,uri);
			mActivity.startActivity(i);
			break;
		case CONTACT:
			MyContactModel model = ModelManager.getInstatce(mActivity).getMyContactModel();
			MyContact data = new MyContact(mContact.getName(), mContact.getPhone());
			model.save(data);
			uri = ContentUris.withAppendedId(
	        		RawContacts.CONTENT_URI, model.get()[0].getId());
	    	i = new Intent(Intent.ACTION_EDIT,uri);
	    	mActivity.startActivity(i);
	    	break;
		case DELETE:
			ModelManager.getInstatce(mActivity).getCallHistoryModel().delete(mHistory.getId());
			mActivity.initListView();
			Toast.makeText(mActivity, mActivity.getString(R.string.msg_history_deleted), Toast.LENGTH_SHORT).show();
			break;
		default:
			break;
		}
		dialog.cancel();
	}
}
